package dao.impl;

import dao.AdminDao;
import dao.BaseDao;
import entity.Admin;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl extends BaseDao implements AdminDao {
    @Override
    public Admin verify(String discount, String password) {
        String sql = "SELECT * FROM admin WHERE discount = ? AND `password` = ?";
        ResultSet rs = executeQuery(sql, discount, password);
        try {
            if (rs.next()) {
                Admin admin = new Admin();
                admin.setId(rs.getInt(1));
                admin.setDiscount(rs.getString(2));
                admin.setPassword(rs.getString(3));
                return admin;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Admin findId(int id) {
        String sql = "SELECT * FROM admin WHERE id=?";
        ResultSet rs = executeQuery(sql, id);
        try {
            if (rs.next()) {
                Admin admin = new Admin();
                admin.setId(rs.getInt(1));
                admin.setDiscount(rs.getString(2));
                admin.setPassword(rs.getString(3));
                return admin;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int addAll(Admin admin) {
        String sql = "INSERT INTO admin(discount,`password`) VALUES (?,?)";
        return executeUpdate(sql, admin.getDiscount(), admin.getPassword());
    }

    @Override
    public int update(Admin admin) {
        String sql = "UPDATE admin SET discount=?,`password`=? WHERE id=?";
        return executeUpdate(sql, admin.getDiscount(), admin.getPassword(), admin.getId());
    }

    @Override
    public List<Admin> findAll() {
        String sql = "SELECT * FROM admin";
        List<Admin> list = new ArrayList<>();
        ResultSet rs = executeQuery(sql);
        try {
            while (rs.next()) {
                Admin admin = new Admin();
                admin.setId(rs.getInt(1));
                admin.setDiscount(rs.getString(2));
                admin.setPassword(rs.getString(3));
                list.add(admin);
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int delById(int id) {
        String sql = "DELETE FROM admin WHERE id=?";
        return executeUpdate(sql, id);
    }

    @Override
    public List<Admin> findAllByName(String name) {
        String sql = "SELECT * FROM admin WHERE discount LIKE ?";
        List<Admin> list = new ArrayList<>();
        ResultSet rs = executeQuery(sql, "%"+name+"%");
        try {
            while (rs.next()) {
                Admin admin = new Admin();
                admin.setId(rs.getInt(1));
                admin.setDiscount(rs.getString(2));
                admin.setPassword(rs.getString(3));
                list.add(admin);
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
